package com.useeinfo.oa.modules.wuHanCommon.base.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;

/**
 * 自动抄表数据表
 */
@Entity
@Table(name = "wuhan_automatic_table")
public class AutomaticTable extends SuperClassEntity<AutomaticTable> {

    /**
     * 所属点位
     */
    @ManyToOne
    @JoinColumn(name = "point_id")
    private WuHanPoint wuHanPoint;
    /**
     * 值
     */
    private String value;
    /**
     * 机组
     */
    private String number;
    /**
     * 抄表时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date autoTime;
    /**
     * 是否异常
     */
    private boolean isAbnormal;
    /**
     * 阈值
     */
    private String infoDetail;

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();

        WuHanPoint wuHanPoint = CommonSugar.getTypedDefault(getWuHanPoint(), new WuHanPoint());
        WuHanUnit wuHanUnit = CommonSugar.getTypedDefault(wuHanPoint.getWuHanUnit(), new WuHanUnit());

        jsonObject.put("id", CommonSugar.getTypedDefault(getId(), 0L));
        jsonObject.put("value", CommonSugar.getTypedDefault(getValue(), ""));
        jsonObject.put("pointName", CommonSugar.getTypedDefault(wuHanPoint.getName(), ""));
        jsonObject.put("pointSystem", wuHanUnit.getPointSystem() == null ? "" : wuHanUnit.getPointSystem().getName());
        jsonObject.put("remark", CommonSugar.getTypedDefault(wuHanPoint.getRemark(), ""));
        jsonObject.put("unit", CommonSugar.getTypedDefault(wuHanUnit.getUnit(), ""));
        jsonObject.put("unitOfMeasurement", CommonSugar.getTypedDefault(wuHanPoint.getUnitOfMeasurement(), ""));
        jsonObject.put("meterReadingName", wuHanUnit.getMeterReadingEnum() == null ? "" : wuHanUnit.getMeterReadingEnum().getName());
        jsonObject.put("autoTime", CommonSugar.getTypedDefault(DateTimeUtils.format(getAutoTime(), "yyyy-MM-dd HH:mm"), ""));
        jsonObject.put("threshold", CommonSugar.getTypedDefault(getInfoDetail(), ""));
        jsonObject.put("isRed", isAbnormal);

        return jsonObject;
    }

    public AutomaticTable() {
    }

    public AutomaticTable(WuHanPoint wuHanPoint, String value, Date autoTime, boolean isAbnormal, String infoDetail) {
        this.wuHanPoint = wuHanPoint;
        this.value = value;
        this.autoTime = autoTime;
        this.isAbnormal = isAbnormal;
        this.infoDetail = infoDetail;
    }

    public WuHanPoint getWuHanPoint() {
        return wuHanPoint;
    }

    public void setWuHanPoint(WuHanPoint wuHanPoint) {
        this.wuHanPoint = wuHanPoint;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Date getAutoTime() {
        return autoTime;
    }

    public void setAutoTime(Date autoTime) {
        this.autoTime = autoTime;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public boolean isAbnormal() {
        return isAbnormal;
    }

    public void setAbnormal(boolean abnormal) {
        isAbnormal = abnormal;
    }

    public String getInfoDetail() {
        return infoDetail;
    }

    public void setInfoDetail(String infoDetail) {
        this.infoDetail = infoDetail;
    }
}
